// 测试所有polyfills
import { Buffer } from 'buffer';
// @ts-ignore
import process from 'process';
// @ts-ignore
import crypto from 'crypto-browserify';
import { authenticator } from 'otplib';

// 设置全局变量
if (typeof globalThis !== 'undefined') {
  globalThis.Buffer = Buffer;
  globalThis.process = process;
}

const MY_SECRET = 'vuixppgt3kx3fbtjp2pzgfzzccxqkv7q';

console.log('=== 测试所有polyfills ===');
console.log('Buffer是否可用:', typeof Buffer !== 'undefined');
console.log('process是否可用:', typeof process !== 'undefined');
console.log('crypto是否可用:', typeof crypto !== 'undefined');
console.log('crypto.createHmac是否可用:', typeof crypto.createHmac === 'function');
console.log('globalThis.Buffer是否可用:', typeof globalThis.Buffer !== 'undefined');
console.log('globalThis.process是否可用:', typeof globalThis.process !== 'undefined');

function testAllPolyfills() {
  try {
    // 测试Buffer功能
    const testBuffer = Buffer.from('test', 'utf8');
    console.log('Buffer测试:', testBuffer.toString());
    
    // 测试process功能
    console.log('process.env测试:', process.env.NODE_ENV || 'undefined');
    
    // 测试crypto功能
    const hmac = crypto.createHmac('sha1', 'test');
    hmac.update('data');
    const digest = hmac.digest('hex');
    console.log('crypto.createHmac测试:', digest);
    
    // 测试otplib
    const code = authenticator.generate(MY_SECRET);
    console.log('生成的TOTP:', code);
    
    const remaining = authenticator.timeRemaining();
    console.log('剩余时间:', remaining, '秒');
    
    console.log('\n=== 所有polyfills测试成功！===');
    
  } catch (error) {
    console.error('测试失败:', error);
  }
}

testAllPolyfills();
